/**
 * @file 预约时间
 * @author yangjiangming <449654337@qq.com>
 * @date 2018/12/14 16:38:57
 */

layui.define(["table", "zmnUtils"], function (exports) {
    //var zmnTable = layui.zmnTable;
    var table = layui.table;
    var zmnUtils = layui.zmnUtils;
    var promotionTable;
    var DISTRIBUTE_RULE_SIMILAR = 3010; //已派相似
    var DISTRIBUTE_RULE_MULTI = 3020; //未派相似
    var DISTRIBUTE_RULE_FIRST = 3030; //首轮优先
    var DISTRIBUTE_RULE_RECOMMEND = 3040; //权重分
    var DISTRIBUTE_RULE_FAIR = 3050; // 公平
    var DISTRIBUTE_RULE_FAIR_REWORK = 3060; // 返修公平
    var DISTRIBUTE_RULE_PROTECTION_ORDINARY = 3070; // 多轮保护-普通
    var DISTRIBUTE_RULE_PROTECTION_PRIORITY = 3080; // 多轮保护-优先
    var DISTRIBUTE_RULE_GRADING_RECOMMEND= 3090;// 分级派
    var DISTRIBUTE_RULE_HOME_APPLIANCES_FAIR= 3100;// 家电突破-公平
    var DISTRIBUTE_RULE_REWORK_PRIORITY = 1010; // 返修优先字段派

    var DISTRIBUTE_RULE_FAIL = -1080;// 未找到满足工程师
    var allMasterList = json.masterList || [];

    var templeteFormatter = {
        masterNameTempleteFunc: function (d) {
            var text = d.masterName + '—' + zmnUtils.maskPhone(d.masterPhone) + '—V' + d.hierarchyLevel;

            return (`
               <div>
                   <div>
                        <span>${text}</span>
                        ${d.distributeRuleTitle ? `<span class="layui-badge layui-bg-green">${d.distributeRuleTitle}</span>` : ''}
                        ${d.novice ? `<span style="background-color: #669900 !important;" class="layui-badge">新手</span>` : ``}
                        ${d.channelExclusive ? `<span style="background-color: #FF6699 !important;" class="layui-badge">专属</span>` : ``}
                        ${d.oaStatus == 1 ? `<span style="background-color: #00bfbf !important;" class="layui-badge">试用</span>` : ``}
                        ${d.priorityType == 1 ? `<span style="background-color: #70b603 !important;" class="layui-badge">源</span>` : ``}
                        ${d.priorityType == 2 ? `<span style="background-color: #70b603 !important;" class="layui-badge">最后</span>` : ``}
                    </div>
                </div>
            `);
        }
    }

    //相似自动派 派单快照表格结构
    const failCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: '250',
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: '80',
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: '80',
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: '80',
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkMaxReworkReminderCount",
            title: "最大返修催单",
            minWidth: '120',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkReminderCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "checkBindGridCount",
            title: "绑定支援网格",
            minWidth: '120',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkBindGridCount);
            },
        }
    ];

    //相似自动派 派单快照表格结构
    const simCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: '250',
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: '80',
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: '90',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: '80',
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: '80',
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '118',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    //首轮自动派 派单快照表格结构
    const firstSendCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: 100,
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "distributeNewCount",
            title: "当日首轮",
            minWidth: 110,
            align: "center",
            sort: true,
            templet: function (data) {
                return data.distributeNewCount == 0 ? "是" : "否";
            },
        },
        {
            field: "yesterdayDistributeNewCount",
            title: "昨日派单数量",
            minWidth: 120,
            align: "center",
            sort: true,
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: 80,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '100',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    //权重自动派 派单快照表格结构
    const weightSendCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: 100,
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "recommendScore",
            title: "系统推荐分值",
            sort: true,
            minWidth: 150,
            align: "center",
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.recommendScore
                );
            }
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: 80,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            field: "datumOrderHourRatio",
            title: "单时比",
            sort: true,
            minWidth: 100,
            align: "center",
            templet:  function (data) { return processDatumOrderHourRatio(data.nonDistributeFlag , data.datumOrderHourRatio)}
        },
        {
            field: "handCountRatio",
            title: "手头单占比",
            minWidth: 120,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.handCountRatio + '%'
                );
            }
        },
        {
            field: "distributeNewCount",
            title: "新单派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distributeNewCount
                );
            },
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "starLevelScore",
            title: "星级分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.starLevelScore);
            }
        },
        {
            field: "compositeScore",
            title: "服务分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.compositeScore);
            }
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '100',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    //公平自动派 派单快照表格结构
    const fairCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: 100,
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "datumOrderHourRatio",
            title: "派单当日单时比",
            sort: true,
            minWidth: 150,
            align: "center",
            templet:  function (data) { return processDatumOrderHourRatio(data.nonDistributeFlag , data.datumOrderHourRatio)}
        },
        {
            field: "distributeNewCount",
            title: "今日新单派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distributeNewCount
                );
            },
        },
        {
            field: "yesterdayDistributeNewCount",
            title: "昨日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.yesterdayDistributeNewCount
                );
            }
        },
        {
            field: "successRate",
            title: "成功率",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return getFormatNum(data.nonDistributeFlag, data.successRate, 100, 2) + '%';
            }
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '100',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    //返修公平自动派 派单快照表格结构
    const fairReworkCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: 100,
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "monthReworkCount",
            title: "月返修（他人）",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.monthReworkCount);
            }
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "compositeScore",
            title: "服务分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.compositeScore);
            }
        }
    ];

    //多轮保护自动派 派单快照表格结构PROTECTION
    const fairProtectionCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "distributeNewCount",
            title: "新单派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distributeNewCount
                );
            },
        },
        {
            field: "yesterdayDistributeNewCount",
            title: "昨日派单数量",
            minWidth: 120,
            align: "center",
            sort: true,
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
        },
        {
            field: "compositeScore",
            title: "服务分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.compositeScore);
            }
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '118',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    //分级权重自动派 派单快照表格结构GRADING_RECOMMEND
    const gradingCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "checkMaxReworkCount",
            title: "最大返修单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkCount);
            },
        },
        {
            field: "checkServiceScope",
            title: "服务区域",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkStock",
            title: "工程师库存",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkStock);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: 100,
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "recommendScore",
            title: "系统推荐分值",
            sort: true,
            minWidth: 150,
            align: "center",
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.recommendScore
                );
            }
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: 80,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            field: "datumOrderHourRatio",
            title: "单时比",
            sort: true,
            minWidth: 100,
            align: "center",
            templet:  function (data) { return processDatumOrderHourRatio(data.nonDistributeFlag , data.datumOrderHourRatio)}
        },
        {
            field: "handCountRatio",
            title: "手头单占比",
            minWidth: 120,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.handCountRatio + '%'
                );
            }
        },
        {
            field: "distributeNewCount",
            title: "新单派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distributeNewCount
                );
            },
        },
        {
            grid: "grid",
            field: "sysAcrossGridCount",
            title: "跨网格派单量",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.sysAcrossGridCount);
            },
        },
        {
            field: "yesterdayTiltDistributeCount",
            title: "昨日倾斜派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.yesterdayTiltDistributeCount
                );
            },
        },
        {
            field: "starLevelScore",
            title: "星级分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.starLevelScore);
            }
        },
        {
            field: "compositeScore",
            title: "服务分",
            minWidth: 150,
            align: "center",
            sort: true,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(data.nonDistributeFlag, data.compositeScore);
            }
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '100',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        }
    ];

    // 家电突破-公平自动派 派单快照表格结构
    const homeAppliancesFairCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "onlineStatus",
            title: "在线",
            sort: true,
            minWidth: '80',
            align: "center",
            templet: function (data) {
                return data.onlineStatus == 2
                    ? '<span class="layui-badge layui-bg-blue">在线</span>'
                    : '<span class="layui-badge layui-bg-gray">离线</span>';
            },
        },
        {
            field: "distance",
            title: "距离",
            sort: true,
            align: "center",
            minWidth: '80',
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distance ? data.distance + "KM" : ""
                );
            },
        },
        {
            field: "checkMainSkill",
            title: "主技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMainSkill);
            },
        },
        {
            field: "checkDistributeNewCount",
            title: "每日新单派单量",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkDistributeNewCount);
            },
        },
        {
            field: "checkMaxDayHandNumber",
            title: "最大日手头单",
            minWidth: '115',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDayHandNumber);
            },
        },
        {
            field: "checkMaxHandCount",
            title: "最大手头单",
            minWidth: '100',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxHandCount);
            },
        },
        {
            field: "checkMaxReworkReminderCount",
            title: "最大返修催单",
            minWidth: '120',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkReminderCount);
            },
        },
        {
            field: "checkMaxDeliveryAndWaitPartCount",
            title: "最大拉修待件单",
            minWidth: '130',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxDeliveryAndWaitPartCount);
            },
        },
        {
            field: "stockType",
            title: "最优库存",
            sort: true,
            align: "center",
            minWidth: '118',
            templet: function (data) {
                return checkStockType(data.stockType);
            },
        },
        {
            field: "datumOrderHourRatio",
            title: "派单当日单时比",
            sort: true,
            minWidth: 150,
            align: "center",
            templet:  function (data) { return processDatumOrderHourRatio(data.nonDistributeFlag , data.datumOrderHourRatio)}
        },
        {
            field: "distributeNewCount",
            title: "今日新单派单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.distributeNewCount
                );
            },
        },
        {
            field: "breakoutDistributeCount",
            title: "今日突破产品组新单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.breakoutDistributeCount
                );
            },
        },
        {
            field: "yesterdayBreakoutDistributeCount",
            title: "昨日突破产品组新单量",
            sort: true,
            align: "center",
            minWidth: 150,
            templet: function (data) {
                return nonDistributeAndDistributeFunc(
                    data.nonDistributeFlag,
                    data.yesterdayBreakoutDistributeCount
                );
            },
        },

    ]

    const reworkPriorityCols = [
        {
            field: "masterName",
            title: "工程师",
            minWidth: 270,
            fixed: 'left',
            align: "center",
            templet: templeteFormatter.masterNameTempleteFunc
        },
        {
            field: "agent",
            title: "类型",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterAgent(data.agent);
            },
        },
        {
            field: "orderEngineerType",
            title: "主从工程师",
            minWidth: 70,
            align: "center",
            sort: true,
            templet: function (data) {
                return masterType(data.orderEngineerType);
            },
        },
        {
            field: "checkEngineerSkill",
            title: "技能",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkEngineerSkill);
            },
        },
        {
            field: "checkEngineerStatus",
            title: "状态",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkEngineerStatus);
            },
        },
        {
            field: "checkServiceScope",
            title: "城市",
            minWidth: 100,
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkServiceScope);
            },
        },
        {
            field: "checkMaxReworkReminderCount",
            title: "最大返修催单",
            minWidth: '120',
            align: "center",
            sort: true,
            templet: function (data) {
                return checkServiceScope(data.checkMaxReworkReminderCount);
            },
        },
    ]

    /**
     * 处理成功率
     * @param flag
     * @param num
     * @param div
     * @param decimal
     */
    function getFormatNum(flag, num, div, decimal) {
        return formatDecimal(num, div, decimal);
    }

    function formatDecimal(num, div, decimal) {
        if (num === "" || num == null) {
            return num;
        }
        num = (num / div).toString()
        let index = num.indexOf('.')
        if (index !== -1) {
            num = num.substring(0, decimal + index + 1)
        } else {
            num = num.substring(0)
        }
        return parseFloat(num).toFixed(decimal)
    }

    /**
     * 处理单时比，保留3位小数
     * @param flag
     * @param value
     * @returns {string|*}
     */
    function processDatumOrderHourRatio (flag, value) {
        return changeDecimalBuZero(value, 3);
    }

    /**
     * 单时比小数位处理
     * @param number
     * @param bitNum
     * @returns {string|number}
     */
    function changeDecimalBuZero(number, bitNum) {
        /// 小数位不够，用0补足位数
        number = number/1000;
        var f_x = parseFloat(number);
        if (isNaN(f_x)) {
            return '0.000';
        }
        var s_x = number.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + bitNum) {
            s_x += '0';
        }
        return s_x;
    }

    /*const list = json.masterList.concat(json.nonDistributeMasterList) || [];
    list.unshift(json.autoDistributeMaster);*/
    function renderPromotionTable(list) {
        var cols;
        if (json.distributeRuleName == null) {
            cols = simCols;
        } else {
            switch (json.distributeRule) {
                case DISTRIBUTE_RULE_SIMILAR:
                case DISTRIBUTE_RULE_MULTI:
                    cols = simCols;
                    break;
                case DISTRIBUTE_RULE_FIRST:
                    cols = firstSendCols;
                    break;
                case DISTRIBUTE_RULE_RECOMMEND:
                    cols = weightSendCols;
                    break;
                case DISTRIBUTE_RULE_FAIR:
                    cols = fairCols;
                    break;
                case DISTRIBUTE_RULE_FAIR_REWORK:
                    cols = fairReworkCols;
                    break;
                case DISTRIBUTE_RULE_PROTECTION_ORDINARY:
                case DISTRIBUTE_RULE_PROTECTION_PRIORITY:
                    cols = fairProtectionCols;
                    break;
                case DISTRIBUTE_RULE_GRADING_RECOMMEND:
                    cols = gradingCols;
                    break;
                case DISTRIBUTE_RULE_HOME_APPLIANCES_FAIR:
                    cols = homeAppliancesFairCols;
                    break;
                case DISTRIBUTE_RULE_FAIL:
                    cols = failCols;
                    break;
                case DISTRIBUTE_RULE_REWORK_PRIORITY:
                    cols = reworkPriorityCols;
                    break;
                default:
                    cols = undefined;
            }
        }
        /*if (json.distributeRule == DISTRIBUTE_RULE_SIMILAR || json.distributeRule == DISTRIBUTE_RULE_MULTI || json.distributeRuleName == null) {
            cols = simCols;
        } else if (json.distributeRule == DISTRIBUTE_RULE_FIRST) {
            cols = firstSendCols;
        } else if (json.distributeRule == DISTRIBUTE_RULE_RECOMMEND) {
            cols = weightSendCols;
        } else if (json.distributeRule == DISTRIBUTE_RULE_FAIR) {
            cols = fairCols;
        } else if (json.distributeRule == DISTRIBUTE_RULE_FAIR_REWORK) {
            cols = fairReworkCols;
        } else if (json.distributeRule == DISTRIBUTE_RULE_PROTECTION_ORDINARY) {
            cols = fairProtectionCols;
        }else if (json.distributeRule == DISTRIBUTE_RULE_PROTECTION_PRIORITY) {
            cols = fairProtectionCols;
        }else if (json.distributeRule == DISTRIBUTE_RULE_GRADING_RECOMMEND) {
            cols = gradingCols;
        }*/
        if (!cols) {
            return;
        }
        cols = cols.filter((item) => {
            return !item.grid || json.distributeWay == 10;
        });
        promotionTable = table.render({
            elem: "#detailMasterTable",
            data: list,
            cols: [cols],
            skin: 'line',
            limit: 1000,
            done: function (res, curr, count) {
                var re = res.data;
                if (re.length > 0) {
                    for (var i in res.data) {
                        var item = res.data[i];
                        if (item.nonDistributeFlag == 2) {
                            $("td[data-content='" + item.masterName + "']").parent().prop("disabled", true);
                            $("td[data-content='" + item.masterName + "']").parent().attr({
                                style: "background:#F2F2F2;color:#999999",
                            }); //将当前行变成灰色
                        } else if (item.nonDistributeFlag == 3) {
                            $("td[data-content='" + item.masterName + "']").parent().attr({
                                style: "background:#00CCCC;color:#333333",
                            }); //将当前行变成绿色
                        }
                    }
                }
            },
            even: true,
        });
    }

    //触发排序事件
    table.on('sort(detailMasterTable)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        //尽管我们的 table 自带排序功能，但并没有请求服务端。
        //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
        table.reload('detailMasterTable', {
            initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
        });
    });

    $("#masterId").on("input", function (e) {
        //获取input输入的值
        var renderData = allMasterList.filter(function (item) {
            return (
                !String(item.masterName).indexOf(e.delegateTarget.value) ||
                !String(item.masterPhone).indexOf(e.delegateTarget.value)
            );
        });
        renderPromotionTable(renderData);
    });
    /**
     * 直营/非直营
     */
    function masterAgent(agent) {
        if (agent == 1) {
            return "直营";
        } else {
            return "非直营";
        }
    }

    /**
     * 直营/非直营
     */
    function masterType(agent) {
        if (agent == 1) {
            return "主";
        } else {
            return "从";
        }
    }
    /**
     * 判断对号 错号
     */
    function checkStockType(stockType) {
        if (stockType == 0) {
            return "<span class='trueGreen'></span > ";
        } else if (stockType == 2 || stockType == 1) {
            return "<span class='falseRed'></span > ";
        } else {
            return "";
        }
    }

    /**
     * 判断对号 错号
     */
    function checkServiceScope(checkServiceScope) {
        if (checkServiceScope == true) {
            return "<span class='trueGreen'></span > ";
        } else if (checkServiceScope == false) {
            return "<span class='falseRed'></span > ";
        } else {
            return "";
        }
    }

    function nonDistributeAndDistributeFunc(flag, value) {
        if (!value) {
            return "";
        }
        return value;
    }

    function initEvent() {
        //renderPromotionTable(list);
    }

    function initPage() {
        var autoMaster = json.autoDistributeMaster;
        if (autoMaster != null) {
            autoMaster.nonDistributeFlag = 3;
            allMasterList.unshift(autoMaster);
        }

        // 设置不可派单师傅标示
        var resultList = json.nonDistributeMasterList
        if (resultList !== null) {
            resultList.forEach(item => {
                item.nonDistributeFlag = 2;
            })
            allMasterList = allMasterList.concat(resultList);
            renderPromotionTable(allMasterList);
        } else {
            renderPromotionTable(allMasterList);
        }
    }

    function init() {
        initEvent();

        initPage();
    }

    init();

    exports("oms_work_modify_distribute_snapshot");
});
